package zsc.ruanc.practicaltraining.security.handler;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.security.exception.SAuthenticationException;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SAuthenticationEntryPoint extends JSONResult implements AuthenticationEntryPoint {



    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {



        JsonResult data = ResultUtils.error(-13, "你token的验证不通过，可能的原因：\n" +
                "1.token过期\n" +
                "2.token无效或被篡改\n" +
                "3." + e.getMessage());

        //输出
        this.WriteJson(httpServletRequest, httpServletResponse, data);

    }
}
